package com.study.utils;

import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

import java.io.IOException;

/**
 * Created by Administrator on 2017/11/9/009.
 */
public class QiniuUtils {
    //下面的三个参数在七牛云后台可以获得
    private static String ACCESS_KEY = "你的ak";
    private static String SECRET_KEY = "你的sk";
    private static String bucketname = "要上传的空间名称";
    private static Auth auth;
    private static UploadManager uploadManager;

    //密钥配置
    static {
        auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        //自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
        Zone z = Zone.autoZone();
        Configuration c = new Configuration(z);
        //创建上传对象
        uploadManager = new UploadManager(c);
    }

    private static String getUpToken() {
        return auth.uploadToken(bucketname, null, 3600, null);
    }

    public static String upload(String filePath) throws IOException {
        Response res = uploadManager.put(filePath, null, getUpToken());//调用put方法上传
        return (String) res.jsonToMap().get("hash");
    }
}
